package me.xiaosiqi.server_info.task;


import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import me.xiaosiqi.server_info.tool.IpTool;
import me.xiaosiqi.server_info.tool.ServerChanService;
import me.xiaosiqi.server_info.tool.ServerDescTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class CheckGatewayTask {
    @Autowired
    private ServerChanService serverChanService;

    //网卡名
    @Value("${spring.ethName}")
    private  String ethName;
    /**
     * 从第0秒开始，每30秒执行一次
     */
    @Scheduled(cron = "0/30 * * * * ? ")
    private void checkGateway() {
        Map<String, List<String>> ipInfo = IpTool.getIpInfo();
        log.info("系统获取到的ip地址集合:{}",ipInfo);
        List<String> ipList = ipInfo.get(ethName);
        if (CollectionUtil.isNotEmpty(ipList)){
            if (IpTool.CheckIpChange(ipList.toString())) {
                //ip没有变化
            }else {
                //ip已经变化
                String info = ServerDescTool.getNetInfo(ipList);
                serverChanService.sendMsg("服务器ip已经变化",info);
                IpTool.updataIpInfo(ipList.toString());
            }
        }else {
            log.error("网卡名称错误，请检查网卡名是否配置正确！");
        }
    }
}
